package com.jhhc.Liunx.Util;

import lombok.extern.slf4j.Slf4j;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import oshi.SystemInfo;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.hardware.Sensors;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.*;


import com.jhhc.utils.SigarUtil;

@Slf4j
public class GetLiunxInfo {




    static SystemInfo si = new SystemInfo();
    static HardwareAbstractionLayer hal = si.getHardware();


    //服务器名称
    public static String getHostName() {
        try {
            return InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {

            log.error("获取服务器名称异常", e);
        }
        return "未知";
    }

    //得到本机所有的物理网络和虚拟机等利用本机的物理网络接口建立的逻辑网络的信息
    public static String getHostIp() {
        try {
            // 得到本机所有的物理网络接口和虚拟机等软件利用本机的物理网络接口创建的逻辑网络接口的信息
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = networkInterfaces.nextElement();
                if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
                    continue;
                }
                Enumeration<InetAddress> inetAddresses = netInterface.getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    InetAddress inetAddress = inetAddresses.nextElement();
                    // TODO:这里IPV4!后面IPV6可能要追加
                    if (inetAddress != null) {
                        String ip = inetAddress.getHostAddress();
                        // 排除虚拟IP
                        if (!ip.endsWith(".1")) {
                            return ip;
                        }
                    }
                }
            }
        } catch (SocketException e) {
            log.error("获取IP地址异常", e);
        }
        return "127.0.0.1";
    }

    //
    public static Map<?, ?> getCpuInfo() throws InterruptedException, SigarException {


        Map<String, Object> map = new HashMap<>();
            Properties props = System.getProperties();
            String systemName = props.getProperty("os.name");

            Sigar sigar = new Sigar();
            org.hyperic.sigar.CpuInfo[] infos = sigar.getCpuInfoList();
            org.hyperic.sigar.CpuInfo infoss = infos[0];
            CpuPerc cpu = sigar.getCpuPerc();
            Integer totalCPUs = infoss.getTotalCores();
            String CPUinfo = infoss.getVendor() + "  " + infoss.getModel();
            double referenceSpeed = infoss.getMhz();
            String CPUSpeed = String.format("%.2f", referenceSpeed / 1000) + " GHz";
            double cpuUsedPerc = cpu.getCombined();
            String CPUPers = "";
            if(systemName.startsWith("win") || systemName.startsWith("Win")) {
                CPUPers = String.format("%.1f", cpuUsedPerc * 100) + "%";
            }else {
                CPUPers = String.format("%.1f", cpuUsedPerc * 1000) + "%";
            }

            map.put("CPU信息",CPUinfo);
            map.put("CPU总数",totalCPUs);
            map.put("CPU基准速度",CPUSpeed);
            map.put("CPU利用率",CPUPers);

            log.info("CPU信息 = {}，CPU总数 = {},CPU基准速度 ={},CPU利用率 ={}",CPUinfo,totalCPUs,CPUSpeed,CPUPers);
            Thread.sleep(1000);
            sigar.close();


        return map;
    }


    //获取内存
    public static  String getMemory(){
        Sigar sigar = new Sigar();
        double memTotal = 0;
        try {
            memTotal = sigar.getMem().getTotal();
        } catch (SigarException e) {
            e.printStackTrace();
        }

        return String.format("%.0f", memTotal / 1024 / 1024 / 1024) + " GB";
    }

    //获取内存使用率
    public static  String getMemoryUsage(){
        Sigar sigar = new Sigar();
        try {
            double memUsedPerc = sigar.getMem().getUsedPercent();
            return String.format("%.2f", memUsedPerc) + " %";
        } catch (SigarException e) {
            e.printStackTrace();
        }
        return  "0.0%";
    }

    public  static  Map<String, Object> getDiskInfo(){
        Map<String, Object> drive =  new HashMap<>();


        oshi.software.os.OperatingSystem op =  si.getOperatingSystem();
        FileSystem fileSystem = op.getFileSystem();
        List<OSFileStore> fsArray = fileSystem.getFileStores();
        for (OSFileStore fs : fsArray) {
            drive = new HashMap<>();

            long free = fs.getUsableSpace();
            long total = fs.getTotalSpace();
            long used = total - free;

            drive.put("name",fs.getName());//盘符名称

            drive.put("driveUses",((int)(((total*1.0)/1024/1024/1024))));//盘符总大小
            drive.put("driveUsed",((int)(((used*1.0)/1024/1024/1024))));//盘使用量
            drive.put("driveUsedLv",(int)((used * 1.0 / total)*100) );//盘使用率

        }




        return drive;

    }
    public static double getCpuTemperature(){
        Sensors sensors = hal.getSensors();
        return  sensors.getCpuTemperature();
    }
}
